<?php
class BPSC extends BP_Group_Extension {
	var $limit;
	var $view_type;
	var $category_id;
	var $display_type;

	function bpsc() {
		global $bp, $groups_template;

		$this->name = __( 'Group Store', 'bpsc' );
		$this->slug = BPSC_GROUP_SLUG;

		$this->display_type = groups_get_groupmeta( $bp->groups->current_group->id, 'bpsc_category_display_type' );
		$this->category_id = groups_get_groupmeta( $bp->groups->current_group->id, 'bpsc_category_id' );

		if ( !$this->category_id || !$this->display_type )
			$this->enable_nav_item = false;
	}

	function create_screen() {
		global $bp, $groups_template;

		if ( !bp_is_group_creation_step( $this->slug ) )
			return false;

		$settings = maybe_unserialize( get_blog_option( BP_ROOT_BLOG, 'bpsc' ) );
		if ( !$settings['groupstoretoggle'] )
			return false;
?>
		<div class="editfield">
			<label for=""><?php _e( 'WP e-Commerce', 'bpsc' ) ?></label>
			<p><?php _e( "List products from a specific store category. This is a great way of letting your group members have straightforward access to products that might interest them.", 'bpsc' ) ?></p>

			<div class="category-wrapper">
				<?php echo $this->get_category_screen( $bp->groups->current_group->id ) ?>
				<?php echo $this->get_category_display_type_screen( $bp->groups->current_group->id ) ?>
			</div>
		</div>
<?php
		wp_nonce_field( 'groups_create_save_' . $this->slug );
	}

	function create_screen_save() {
		check_admin_referer( 'groups_create_save_' . $this->slug );

		$this->method = 'create';
		$this->save();
	}

	function edit_screen() {
		global $bp, $groups_template;

		if ( !bp_is_group_admin_screen( $this->slug ) )
			return false;

		$settings = maybe_unserialize( get_blog_option( BP_ROOT_BLOG, 'bpsc' ) );
		if ( !$settings['groupstoretoggle'] )
			return false;
		?>
		<div class="editfield">
			<label for=""><?php _e( 'Store', 'bpsc' ) ?></label>
			<p><?php _e( "List products from a specific store category. This is a great way of letting your group members have straightforward access to products that might interest them.", 'bpsc' ) ?></p>

			<div class="category-wrapper">
				<?php echo $this->get_category_screen( $bp->groups->current_group->id ) ?>
				<?php echo $this->get_category_display_type_screen( $bp->groups->current_group->id ) ?>
			</div>
		</div>

		<div class="form-submit">
			<br />
			<input type="submit" name="save" value="<?php _e( "Update", 'bpsc' ) ?>" />
		</div>
<?php
		wp_nonce_field( 'groups_edit_save_' . $this->slug );
	}

	function edit_screen_save() {
		global $bp;

		if ( !isset( $_POST['save'] ) )
			return false;

		check_admin_referer( 'groups_edit_save_' . $this->slug );

		$this->method = 'edit';
		$this->save();
	}

	function get_category_display_type_screen( $group_id ) {
		global $bp, $wpdb;

		$form  = '<label for="editable-group-display-type">' . __( 'Display the products as:', 'bpsc' ) . '</label>';
		$form .= '<select id="editable-group-display-type" name="editable-group-display-type">';

		$selected = '';
		$types = array( 'default' => __( "with pictures and product description", 'bpsc' ), 'list' => __( "a list", 'bpsc' ), 'grid' => __( "in a grid", 'bpsc' ) );
		foreach ( $types as $k => $v ) {
			if ( $this->display_type && $k == $this->display_type )
				$selected = ' selected';
			else
				$selected = '';

			$form .= '<option value="' . $k . '" ' . $selected . '>' . $v . '</option>';
		}

		$form .= '</select>';
		return apply_filters( 'bpsc_get_category_display_type_screen', $form, $group_id );
	}

	function get_category_screen( $group_id ) {
		global $bp, $wpdb;

		$form  = '<label for="editable-group-category">' . __( 'Products from this category:', 'bpsc' ) . "</label>";
		$form .= '<select id="editable-group-category" name="editable-group-category">';
		$form .= '<option value="0">----</option>';

		// pgibbs: Does WPSC have a method to get this directly?
		//3.8: $categories = $wpdb->get_results( $wpdb->prepare( "SELECT meta_id as id, meta_value FROM `" . WPSC_TABLE_META . "` WHERE `object_type` = 'wpsc_category' AND `meta_key` = 'nicename'" ) );
    $categories = $wpdb->get_results( "SELECT `id`, `name` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` WHERE `active`='1' AND `category_parent` = '0' ORDER BY `nice-name` ASC" );
		foreach ( $categories as $category ) {
			if ( $this->category_id && $category->id == $this->category_id )
				$selected = ' selected';
			else
				$selected = '';

			$form .= '<option value="' . esc_attr( $category->id ) . '"' . $selected . '>' . apply_filters( 'bpsc_get_group_category_name', $category->name ) . '</option>';
		}

		$form .= '</select>';
		return apply_filters( 'bpsc_get_category_screen', $form, $group_id, $category_id );
	}

	function save() {
		global $bp;

		$settings = maybe_unserialize( get_blog_option( BP_ROOT_BLOG, 'bpsc' ) );
		if ( !$settings['groupstoretoggle'] )
			return;

		if ( !$group_id = $_POST['group_id'] )
			$group_id = $bp->groups->current_group->id;

		/* Set error redirect based on save method */
		if ( $this->method == 'create' )
			$redirect_url = $bp->root_domain . '/' . $bp->groups->slug . '/create/step/' . $this->slug;
		else
			$redirect_url = bp_get_group_permalink( $bp->groups->current_group ) . 'admin/' . $this->slug;

		if ( empty( $_POST['editable-group-category'] ) )
			$_POST['editable-group-category'] = 0;

		if ( empty( $_POST['editable-group-display-type'] ) )
			$_POST['editable-group-display-type'] = 'default';

		$display_type = apply_filters( 'bpsc_group_validate_display_type', $_POST['editable-group-display-type'] );
		if ( !in_array( $_POST['editable-group-display-type'], array( 'list', 'grid', 'default' ) ) )
			$display_type = apply_filters( 'bpsc_group_validate_display_type', 'default' );

		$category_id = apply_filters( 'bpsc_group_validate_category_id', $_POST['editable-group-category'] );
		groups_update_groupmeta( $group_id, 'bpsc_category_id', $category_id );
		groups_update_groupmeta( $group_id, 'bpsc_category_display_type', $display_type );

		if ( $this->method != 'create' ) {
			$redirect_url = bp_get_group_permalink( $bp->groups->current_group ) . 'admin/' . $this->slug;
			bp_core_add_message( __( 'The Group Store settings have been updated.', 'bpsc' ), 'success' );
			bp_core_redirect( $redirect_url );
			exit();
		}
	}

	function display() {
		$_SESSION['wpsc_display_type'] = $_GET['view_type'] = $this->display_type;
	?>
		<div id="item-body">
			<div class="pagination no-ajax">
				<p><?php echo wpsc_category_name( $this->category_id ) ?></p>
			</div>

			<?php echo wpsc_display_products_page( array( 'category_id' => $this->category_id, 'limit_of_items' => 8 ) ) ?>
		</div>
	<?php
	}
}

bp_register_group_extension( 'BPSC' );
?>